﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Net;

namespace xml
{
    public partial class Manager : Form
    {
        Rss rss;
        Voice voice = new Voice();
        public Manager()
        {
            InitializeComponent();
        }
        void Manager_MouseWheel(object sender, MouseEventArgs e)
        {
            Point aPoint = new Point(e.X, e.Y);
            aPoint.Offset(this.Location.X, this.Location.Y);
            Rectangle aRec1 = new Rectangle(flowLayoutPanel.Location.X, flowLayoutPanel.Location.Y, flowLayoutPanel.Width, flowLayoutPanel.Height);

            if (RectangleToScreen(aRec1).Contains(aPoint))
                flowLayoutPanel.AutoScrollPosition = new Point(0, flowLayoutPanel.VerticalScroll.Value - e.Delta / 80);


        }

        private string InputBox(string Caption, string Hint, string Default)
        {
            Form InputForm = new Form();
            InputForm.MinimizeBox = false;
            InputForm.MaximizeBox = false;
            InputForm.StartPosition = FormStartPosition.CenterScreen;
            InputForm.Width = 220;
            InputForm.Height = 150;
            InputForm.Text = Caption;
            Label lbl = new Label();
            lbl.Text = Hint;
            lbl.Left = 10;
            lbl.Top = 20;
            lbl.Parent = InputForm;
            lbl.AutoSize = true;
            TextBox tb = new TextBox();
            tb.Left = 30;
            tb.Top = 45;
            tb.Width = 160;
            tb.Parent = InputForm;
            tb.Text = Default;
            tb.SelectAll();
            Button btnok = new Button();
            btnok.Left = 30;
            btnok.Top = 80;
            btnok.Parent = InputForm;
            btnok.Text = "确定";
            InputForm.AcceptButton = btnok;//回车响应
            btnok.DialogResult = DialogResult.OK;
            Button btncancal = new Button();
            btncancal.Left = 120;
            btncancal.Top = 80;
            btncancal.Parent = InputForm;
            btncancal.Text = "取消";
            btncancal.DialogResult = DialogResult.Cancel;
            try
            {
                if (InputForm.ShowDialog() == DialogResult.OK)
                {
                    return tb.Text;
                }
                else
                {
                    return null;
                }
            }
            finally
            {
                InputForm.Dispose();
            }

        }
        private void GetRSSUrlBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string ib = InputBox("输入地址", "请输入URL:", "");

                if (ib != null)
                {
                    if (!ib.StartsWith("http://"))
                    {
                        ib = "http://" + ib;
                    }

                    WebRequest request = WebRequest.CreateDefault(new Uri(ib));
                    WebResponse response = request.GetResponse();
                    rss = Rss.Load(response.GetResponseStream());
                    LayoutAll();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void LayoutAll()
        {
            int num = 1;
            flowLayoutPanel.Controls.Clear();
            voice.Stop();
            if (VoiceBtn.Checked == true)
            {
                foreach (Item item in rss.channel.items)
                {
                    Information information = new Information(item.title, item.author, item.description, item.pubDate, item.link, num);

                    voice.Read(num.ToString() + "。" + item.title);

                    flowLayoutPanel.Controls.Add(information);
                    num++;
                }
            }
        }
        private void GetLocalRSSFileBtn_Click(object sender, EventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Application.StartupPath;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                rss = Rss.Load(ofd.FileName); 
                LayoutAll();
            }


        }



    }
}
